<?php
//******************************************************************
function __autoload($className) {
	if (!isset($GLOBALS['possibleClasses'])) {
		$cacheFile = ROOT_PATH."cache/autoload.cache";
		if (AUTOLOAD_CACHE) {
			if (!($GLOBALS['possibleClasses'] = unserialize(@file_get_contents($cacheFile)))) {
				$GLOBALS['possibleClasses'] = getPossibleClasses();
				file_put_contents($cacheFile, serialize($GLOBALS['possibleClasses']));
			}
		} else {
			$GLOBALS['possibleClasses'] = getPossibleClasses();
		}
	}

	if ($require = current(preg_grep("/^.*".$className."\.php$/is", $GLOBALS['possibleClasses']))) {
		require_once($require);
		return true;
	}
	return false;
}
//******************************************************************
function getPossibleClasses() {
	return array_merge(
		getFileList(ROOT_PATH.'core/'),
		getFileList(ROOT_PATH.'modules/'),
		getFileList(ROOT_PATH.'widgets/'),
		getFileList(ROOT_PATH.'libs/')
	);
}
//******************************************************************
define('DB_DATETIME_FORMAT', '%Y-%m-%d %H:%M:%S');
//******************************************************************
define('ROLE_SUPERADMIN', 'superadmin');
define('ROLE_ADMIN', 'admin');
define('ROLE_USER', 'user');
define('ROLE_GUEST', 'guest');
//******************************************************************
define('PAGE_ALERT_SUCCESS', 'success');
define('PAGE_ALERT_ERROR', 'error');
//******************************************************************
$GLOBALS['userPrimaryProperties'] = array(
	'id' => "",
	'login' => '/^[A-Z0-9._-]+@[A-Z0-9][A-Z0-9.-]{0,61}[A-Z0-9]\.[A-Z.]{2,6}$/i',
	'password' => "",
	'role' => "",
	'last_activity' => "",
	'is_active' => ""
);
$GLOBALS['userSecondaryProperties'] = array(
	'name' => "",
	'company' => "",
	'preferred_theme' => "",
	'preferred_language' => "",
	'group' => "",
);
//******************************************************************
$GLOBALS['urlAliases'] = array(
	'absolute' => array(
		'/admin/' => '/simplePagesEdit/',
		'/admin' => '/simplePagesEdit/',
		'/' => '/simplePages/main/'
	),
	'partial' => array(
		'/admin/' => '/',
		'/text/' => '/simplePages/',
		'/page/' => '/simplePages/',
		'/catalogue/' => '/treePages/',
		'/files/' => '/filesList/'
	)
);
//******************************************************************
function showDebugInfo($engine, $script_start_time, $script_end_time) {
	echo PHP_EOL.PHP_EOL.'<fieldset><legend><b>Debug Info</b></legend>';
	
	echo '<div id="debuginfo">';
	echo 'Generation time: <i>'.number_format(($script_end_time - $script_start_time), 5).' sec</i><br/>';
	
	echo 'Included files (<i>'.count(get_included_files()).'</i>):';
	echo '<pre>';
	foreach (get_included_files() as $k=>$v) echo '#'.$k.":\t".$v.'<br/>';
	echo '</pre>';
	
	echo 'XSL templates (<i>'.count($engine->getTemplatesList()).'</i>):';
	echo '<pre>';
	foreach ($engine->getTemplatesList() as $k=>$v) echo '#'.$k.":\t".$v.'<br/>';
	echo '</pre>';
	
	echo 'SQL queries (<i>'.count($engine->sql->history).'</i>):';
	echo '<pre>';
	foreach ($engine->sql->history as $v) {
		echo '('.number_format($v['time'], 6)."):\t".$v['query'].'<br/>';
		if (isset($v['error'])) {
			echo "\t\t".'<span style="color:#cc0000;">'.$v['error'].'</span><br/>';
		}
	}
	echo '</pre>';

	echo '<pre>';
	var_dump($engine);
	echo '</pre>';
	
	echo '</div>';
	echo '</fieldset>';
}
//******************************************************************
?>